<?php
class DatabaseObject_Circle extends DatabaseObject {
	protected $db = null;
	public function __construct($db) {
		$this->db = $db;
		parent::__construct($this->db, 'circles', 'circleid');
		$this->add('circlename', '');
		$this->add('userid', 0);
		$this->add('username', '');
		$this->add('description', '');
		$this->add('dateline', 0);
		$this->add('climit', 0);
	}
	
    public function addMember($userid = 0, $type = 0) {
    	if ($userid == 0 ) {
    		$userid = $this->userid;
    	}
    	
		$query = sprintf('select * from circle_user 
		      where circleid=%d and userid=(?)', $this->getId());
		$query = $this->db->quoteInto($query, $userid);
		$count = $this->db->fetchOne($query);
		
		if ($count != 0) {
			return;
		}
		$table = 'circle_user';
		$row = array('circleid' => $this->getId(), 
		             'userid'   => $userid,
		             'dateline' => time(),
		             'title'    => $type);
        $this->db->insert($table, $row);
		return true;
	}
	
	public function postDelete() {
		$table = 'circle_user';
		$where = $this->db->quoteInto('circleid=(?)', $this->getId());
		$this->db->delete($table, $where);
		return true;
	}
	
	public function getUserInfo($userid) {
		$query = sprintf('select * from circle_user 
		      where circleid=%d and userid=(?)', $this->getId());
		$query = $this->db->quoteInto($query, $userid);

		$res = $this->db->fetchAll($query);
		return $res[0];
	}
	
	public function loadMember() {
		$query = sprintf('select u.username, cu.* from circle_user as cu
		      , users as u where cu.userid = u.userid and circleid=(%d)', $this->getId());
		$data = $this->db->fetchAll($query);
		return $data;
	}
}
?>